package post

import (
	"context"

	"mall/common/globalkey"
	"mall/common/jwt"
	"mall/common/resultx"
	"mall/service/forum/api/internal/logic/util"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/forum/rpc/forumclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddForumPostFavoriteLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddForumPostFavoriteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddForumPostFavoriteLogic {
	return &AddForumPostFavoriteLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddForumPostFavoriteLogic) AddForumPostFavorite(
	req *types.AddForumPostFavoriteReq,
	headers *[]*types.Header,
) (*types.BaseMsgResp, error) {
	if req.PostId == nil || *req.PostId == 0 {
		return nil, resultx.NewErrCode(resultx.FORUM_REACTION_CONTENT_ID_INVALID)
	}

	if req.Type == nil || *req.Type == 0 {
		return nil, resultx.NewErrCode(resultx.FORUM_REACTION_TYPE_INVALID)
	}

	userId := jwt.GetUserId(l.ctx)
	if userId == 0 {
		return nil, resultx.NewErrCode(resultx.TOKEN_EXPIRE_ERROR)
	}

	_, err := l.svcCtx.ForumRpc.LogReaction(l.ctx, &forumclient.LogReactionReq{
		PostId: req.PostId,
		UserId: userId,
		Type:   util.GetForumReactionType(*req.Type),
		Status: uint32(globalkey.StatusEnabled),
	})
	if err != nil {
		return nil, err
	}

	return &types.BaseMsgResp{
		Msg: "操作成功",
	}, nil
}
